import React from 'react';
import { Toast,InputItem } from 'antd-mobile';

export default class Index extends React.Component {
    constructor(props){
        super(props)
        this.state={
            pwd1: '',
            pwd2: '',
            code: '',
			codetxt: '立即获取',
			code_btn: false,
        }
		this.timer=null
    }
	componentDidMount(){
		document.title = '修改登录密码'
    }
	componentWillUnmount(){
		if(this.timer){
			clearInterval(this.timer)
		}
	}
	// 发送验证码
	send_sms(){
		if(this.state.code_btn){
			return;
		}
		var time=120;
		this.setState({
			code_btn: true,
		})
		Toast.loading('发送中..')
		var _this=this;
		global.Ajax.hhtc_request("user/send_sms",{}).then(data=>{
			Toast.hide()
			if(data.code===1){
				Toast.success(data.msg,1)
				this.timer=setInterval(() => {
					if(time>0){
						time--;
						_this.setState({
							codetxt: time+'s',
						})
					}else{
						clearInterval(_this.timer)
						_this.setState({
							code_btn: false,
							codetxt: '重新获取',
						})
						time=60;
					}
				}, 1000);
			}else{
				Toast.fail(data.msg, 1);
				this.setState({
					code_btn: false,
				})
			}
		})
	}
    save(){
        if(this.state.pwd1.length<6||this.state.pwd1.length>16){
            return Toast.fail('密码长度为6-16位!',1)
        }
        if(this.state.pwd1!==this.state.pwd2){
            return Toast.fail('两次密码不一致!',1)
        }
        if(this.state.code.length!==6){
            return Toast.fail('请输入短信验证码!',1)
        }
        Toast.loading()
        var req={
            password: this.state.pwd1,
            password1: this.state.pwd2,
            smscode: this.state.code,
        }
        global.Ajax.hhtc_request("user/editpass",req).then(data=>{
            Toast.hide()
			if(data.code===1){
				Toast.success(data.msg,1,()=>{
                    this.props.history.goBack()
                })
			}else{
                Toast.fail(data.msg,1)
            }
		})
    }
	render(){
		return(
			<div className='edit_pwd_box'>
                <p>新密码</p>
                <input type='password' onChange={(e)=>{
                    this.setState({
                        pwd1: e.target.value
                    })
                }} placeholder='请设置您的新密码6-16位' />
                <p>确认密码</p>
                <input type='password' onChange={(e)=>{
                    this.setState({
                        pwd2: e.target.value
                    })
                }} placeholder='请确认密码' />
                <div>
                    <p style={{flex: 1,}}>短信验证码</p>
                    <span onClick={this.send_sms.bind(this)}>{this.state.codetxt}</span>
                </div>
                <input type='number' maxLength={6} onChange={(e)=>{
                    this.setState({
                        code: e.target.value,
                    })
                }} placeholder='请输入短信验证码' />
                <p className='save_set' onClick={this.save.bind(this)}>保存设置</p>
			</div>
		)
	}
}
